<?php
// +++++++++ DEBUG ONLY
error_reporting(E_ALL);
// --------- DEBUG ONLY

/**
 * FMSConfig is used to parse settings.xml file.
 * settings.xml contains environment parameters and
 * files to include. It may also contain custom varibles.
 *
 * @package Core
 * @author Urban Soban
 * @copyright 2009 Urban Soban
 */

class FMSConfig
{
    /**
     * Contains all varibles
     * @var array
     */
    private $Config = array();

    /**
     * Contains SimpleXML object.
     * @var SimpleXML
     */
    private $XmlObject;

    function __construct()
    {
        $this->parseConfigFile();
    }

    /**
     * Loads settings.xml to the {@link FMSConfig::$XmlObject}.
     * @return bool false on failure, true on success
     */
    function init()
    {
        $XmlConfigFilePath = dirname(__file__) . "/settings/settings.xml";

        $this->XmlObject = simplexml_load_file($XmlConfigFilePath);

        if (!$this->XmlObject) {
            throw new Exception("Cannot load < settings/settings.xml >");
        }

        return true;
    }

    /**
     * Parses settings.xml to the {@link FMSConfig::$Config}
     * @return bool false on failure, true on success
     */
    function parseConfigFile()
    {
        $this->init();

        foreach ($this->XmlObject->param as $Parameter) {
            $Attributes = $Parameter->attributes();
            $ParameterName = (string )$Attributes["name"];
            $ParameterType = (string )$Attributes["type"];

            if (!isset($Attributes["type"])) {
                return false;
            }

            $this->Config[$ParameterType][$ParameterName] = array("value" => (string )$Parameter);

            foreach ($Attributes as $AttrName => $AttrValue) {
                if ($AttrName != "name" && $AttrName != "type") {
                    $this->Config[$ParameterType][$ParameterName][(string )$AttrName] = (string )$AttrValue;
                }
            }
        }

        return true;
    }

    /**
     * Returns entire {@link FMSConfig::$Config}
     *
     * @return array
     */
    function getConfig()
    {
        return $this->Config;
    }

    /**
     * Returns all varibles of given type
     *
     * @param string $type
     * @return mixed false on failure, array on success
     */
    function getType($type)
    {
        if (isset($this->Config[$type])) {
            return $this->Config[$type];
        }
        return false;
    }

    /**
     * Searches {@link FMSConfig::$Config} 
     * At least varible type and varible name should be given
     *
     * @param string $type Varible type (e.g. 'environment', 'boot')
     * @param string $name Varible name (given in settings.xml)
     * @param string $attr Optional - varible attribute (e.g. 'label')
     * @return mixed false on failure, string on success
     */
    function getValue($type, $name, $attr = null)
    {
        $attribute = ($attr == null) ? "value" : $attr;

        if (isset($this->Config[$type][$name][$attribute])) {
            return $this->Config[$type][$name][$attribute];
        }

        return false;
    }
}

?>